<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
        <el-form :inline="true"
                 :model="permQueryParam"
                 class="demo-form-inline">
          <el-form-item label="权限名">
            <el-input v-model="permQueryParam.permName"
                      placeholder="权限名"
                      clearable
                      @clear="getPermList"></el-input>
          </el-form-item>
          <el-form-item label="权限等级">
            <el-input v-model="permQueryParam.level"
                      placeholder="权限等级"
                      clearable
                      @clear="getPermList"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"
                       @click="getPermList">查询</el-button>
          </el-form-item>
          <el-button type="primary" @click="addDialogVisible = true" style="float: right" >增加权限</el-button>
        </el-form>
      <!-- 权限列表区域 -->
      <el-table :data="this.pageBean.list"
              ref="multipleTable"
              tooltip-effect="dark"
              style="width: 100%">
        <el-table-column type="selection"  width="55"></el-table-column>
        <el-table-column label="权限名"
                         prop="permName"></el-table-column>
        <el-table-column label="权限等级"
                         prop="level"></el-table-column>
        <el-table-column label="权限描述"
                         prop="desc"></el-table-column>
        <el-table-column label="操作"
                         width="200px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button type="primary"
                       icon="el-icon-edit"
                       size="mini"
                       @click="showEditDialog(scope.row.id)"></el-button>
            <!-- 删除按钮 -->
            <el-button type="danger"
                       icon="el-icon-delete"
                       size="mini"
                       @click="removeUserById(scope.row.id)"></el-button>

            <el-button type="warning"
                        icon="el-icon-setting"
                        size="mini"
                        @click="setRole(scope.row.id)"></el-button>           
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="pageBean.pageNum"
                     :page-sizes="[1, 2, 5, 10]"
                     :page-size="pageBean.pageSize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="pageBean.total">
      </el-pagination>
    </el-card>
    <!-- 添加权限的对话框 -->
    <el-dialog title="添加权限"
               :visible.sync="addDialogVisible"
               width="50%"
               @close="addDialogClosed">
      <!-- 内容主体区域 -->
      <el-form :model="addForm"
               :rules="addFormRules"
               ref="addFormRef"
               label-width="70px">
        <el-form-item label="权限名"
                      prop="permName">
          <el-input v-model="addForm.permName"></el-input>
        </el-form-item>
        <el-form-item label="权限等级"
                      prop="level">
          <el-input v-model="addForm.level"></el-input>
        </el-form-item>
        <el-form-item label="权限描述"
                      prop="desc">
          <el-input v-model="addForm.desc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addPerm">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 修改权限的对话框 -->
    <el-dialog title="修改权限"
               :visible.sync="editDialogVisible"
               width="50%"
               @close="editDialogClosed">
      <el-form :model="editForm"
               :rules="editFormRules"
               ref="editFormRef"
               label-width="70px">
        <el-form-item label="权限名">
          <el-input v-model="editForm.permName"
                    disabled></el-input>
        </el-form-item>
               <el-form-item label="权限等级"
                      prop="level">
          <el-input v-model="editForm.level"></el-input>
        </el-form-item>
        <el-form-item label="权限描述"
                      prop="desc">
          <el-input v-model="editForm.desc"></el-input>
        </el-form-item>
 
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editUserInfo">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      permQueryParam: {  //添加查询参数，对应后端的实体类
        permName: '',
        level: '',
        pageNum: 2,
        pageSize: 8
      },
      pageBean: {  //分页数据
        total: 0,
        pages: 0,
        pageNum: 0,
        pageSize: 0,
        list: [],  //表示后台的role集合
      },
      // 控制添加权限对话框的显示与隐藏
      addDialogVisible: false,
      // 添加权限的表单数据
      addForm: {
        permName: '',
        level: '',
        desc: '',
      },    
      // 控制修改权限对话框的显示与隐藏
      editDialogVisible: false,
      // 查询到的权限信息对象
      editForm: {},
    }
  },
  created () {
    this.getPermList()
  },
  methods: {
    //得到权限列表,并且根据查询条件展示
    getPermList () {
      console.log(this.permQueryParam)
      this.axios.post(this.baseUrl + "perm/getAllPerm",this.permQueryParam)
        .then(res => {
          if (res.data.code == 200) {
            console.log(res.data.data)
            //将数据传给pageBean
            this.pageBean = res.data.data
            console.log(this.pageBean)
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.data.message,
            });
            console.log(res.data.message)
          }
        })
    },
    // 监听 pageSize 改变的事件
    handleSizeChange (newSize) {
      // console.log(newSize)
      console.log(newSize)
      this.permQueryParam.pageSize = newSize
      console.log(this.permQueryParam.pageSize)
      this.getPermList()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange (newPage) {
      console.log(newPage)
      this.permQueryParam.pageNum = newPage
      this.getPermList()
    },
    // 点击按钮，添加新权限
    addPerm () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        // 可以发起添加权限的网络请求
        const { data: res } = await this.axios.post(this.baseUrl+'perm/insert', this.addForm)

        if (res.data.code !== 201) {
          this.$message.error('添加权限失败！')
        }

        this.$message.success('添加权限成功！')
        // 隐藏添加权限的对话框
        this.addDialogVisible = false
        // 重新获取权限列表数据
        this.getPermList()
      })
    },
    //展示编辑框
    showEditDialog (id){
        console.log(id)
        this.axios.get(this.baseUrl+"perm/getById/"+id)
            .then(res=>{
               if (res.data.status === 'SUCCESS') {
                console.log(res.data.data)
               this.editForm = res.data.data
               this.editDialogVisible = true
            this.$message({
              type: 'success',
              message: res.data.message
            });
          } else {
            this.$message({
              type: 'warning',
              message: res.data.message,
            });
            console.log(res.data.message)
          }
            })
    },
    // 监听修改权限对话框的关闭事件
    editDialogClosed () {
      this.$refs.editFormRef.resetFields()
    },
    // 修改编辑框中权限信息并提交
    editUserInfo () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return   //如果无效，直接return
        this.axios.put(this.baseUrl+ 'perm/update/',this.editForm)
                .then(res=>{
                   if (res.data.status ==='SUCCESS') {
                      // 关闭对话框
                      this.editDialogVisible = false
                      // 刷新数据列表
                      this.getPermList()
                      // 提示修改成功
                      console.log(res.data.data)
                      return this.$message.success('更新权限信息成功！')
                    }
                      console.log(res.data.message)
                      this.$message.error('更新权限信息失败！')
                            })   
    })
    },
    // 根据Id删除对应的权限信息
    async removeUserById (id) {
      // 弹框询问权限是否删除数据
      console.log(id);
      const confirmResult = await this.$confirm(
        '此操作将永久删除该权限, 是否继续?',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch(err => err)
      // 如果权限确认删除，则返回值为字符串 confirm
      // 如果权限取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消删除')
      }
      const { data: res } = await this.axios.delete(this.baseUrl +'perm/delete/' + id)
      console.log(res)
      if (res.data.status ==='SUCCESS') {
        this.getPermList()
        return this.$message.error('删除权限成功！')
      }
      this.$message.success('删除权限成功！') 
    },
 
  }
}
</script>

<style lang="less" scoped>
</style>
